package com.gitee.wsl.lang.bits.api

import kotlin.math.max

/*
 * All changes to the original code are Copyright DataStax, Inc.
 *
 * Please see the included license file for details.
 */

/*
 * Original license:
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



/**
 * Interface for Bitset-like structures.
 */
interface Bits {
    /**
     * Returns the value of the bit with the specified `index`.
     *
     * @param index index, should be non-negative and &lt; [.length]. The result of passing
     * negative or out of bounds values is undefined by this interface, **just don't do it!**
     * @return `true` if the bit is set, `false` otherwise.
     */
    operator fun get(index: Int): Boolean

    /** Returns the number of bits in this set  */
    fun length(): Int

    /** Bits with all bits set.  */
    class MatchAllBits(val len: Int = -1) : Bits {
        override fun get(index: Int): Boolean {
            return true
        }

        override fun length(): Int {
            if (len == -1) {
                throw UnsupportedOperationException("Unspecified length")
            }
            return len
        }
    }

    /** Bits with no bits set.  */
    class MatchNoBits(val len: Int = -1) : Bits {
        override fun get(index: Int): Boolean {
            return false
        }

        override fun length(): Int {
            if (len == -1) {
                throw UnsupportedOperationException("Unspecified length")
            }
            return len
        }
    }

    companion object {
        /**
         * Returns a Bits that is true when `bits` is false, and false when `bits` is true
         */
        fun inverseOf(bits: Bits): Bits {
            return object : Bits {
                override fun get(index: Int): Boolean {
                    return !bits[index]
                }

                override fun length(): Int {
                    return bits.length()
                }
            }
        }

        /**
         * Return a Bits that is set for a given ordinal iff both it is set in both `a` and `b`.
         */
        fun intersectionOf(a: Bits, b: Bits): Bits {
            if (a is MatchAllBits) {
                return b
            }
            return if (b is MatchAllBits) {
                a
            } else a as? MatchNoBits
                ?: (b as? MatchNoBits
                    ?: object : Bits {
                        override fun get(index: Int): Boolean {
                            return a[index] && b[index]
                        }

                        override fun length(): Int {
                            return max(a.length().toDouble(), b.length().toDouble()).toInt()
                        }
                    })
        }

        val ALL: Bits = MatchAllBits()
        val NONE: Bits = MatchNoBits()
    }
}

